package com.fuyc.project.tools.netconn;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import com.fuyc.project.tools.annotation.FucyMainMethodTag;
import com.fuyc.project.tools.netconn.entity.ExcelIpBean;
import com.fuyc.project.tools.utils.FuycSystemUtils;
import com.fuyc.project.tools.utils.RemoteSSHUtils;
import com.xuxueli.poi.excel.ExcelExportUtil;
import com.xuxueli.poi.excel.ExcelImportUtil;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NetStrategy {
	
	/**
	 * 最大线程数
	 * */
	private int maxThreads=100;
	
	/**
	 * 待测试数据
	 * */
	private List<Object> ips = new ArrayList<>();
	
	private File netStrategyExcelFile;
	
	/**
	 * 线程池
	 * */
	private ExecutorService executorService;
	
	private AtomicInteger taskCount;
	
	@FucyMainMethodTag
	public void testNetConn(String args[]) throws InterruptedException {
		init();
		exeTask();
		//等待测试
		while(true) {
			Thread.sleep(2000);
			if(this.taskCount.get()<=0) {
				break;
			}
		}
		//关闭线程池
		try {
			executorService.shutdown();
		} catch (Exception e) {
			log.error("线程池关闭失败！");
		}
		
		while(true) {
			if(executorService.isTerminated()) {
				break;
			}
			Thread.sleep(2000);
		}
		
		//执行回写Excel
		
		ExcelExportUtil.exportToFile("D:/test/test.xls", this.ips);
		
	}
	
	/**
	 * 解析启动参数
	 * fuyc.netstrategy.MaxThreads=100
	 * fuyc.netstrategy.ExcelFilePath=/home/outapp/temp/a.xls
	 * fuyc.netstrategy.ResultExcelFilePath=/home/outapp/temp/b.xls
	 * 
	 * */
	private void init() {
		/**
		 * 最大并发数
		 * */
		String maxThreadsStr = FuycSystemUtils.getSysProperty("fuyc.netstrategy.MaxThreads", "100");
		Integer maxThreadCont = Integer.valueOf(maxThreadsStr);
		if(maxThreadCont>0) {
			this.maxThreads=maxThreadCont;
		}
		this.executorService=Executors.newFixedThreadPool(this.maxThreads);

		/**
		 * 待测试Excel路径
		 * */
		String filePath=FuycSystemUtils.getSysProperty("fuyc.netstrategy.ExcelFilePath", "netstrategy/netstrategy.xls");
		File excelFile=null;
		try {
			excelFile=new File(filePath);
			if(!excelFile.isFile()) {
				excelFile=FuycSystemUtils.getClassPathFile("netstrategy/netstrategy.xls");
			}
		} catch (Exception e) {
			excelFile=FuycSystemUtils.getClassPathFile("netstrategy/netstrategy.xls");
		}	
		this.netStrategyExcelFile=excelFile;
		
		this.ips = ExcelImportUtil.importExcel(netStrategyExcelFile,  ExcelIpBean.class);
		taskCount=new AtomicInteger(this.ips.size());
	}

	private void exeTask() {
		for(Object o:ips) {
			ExcelIpBean excelIpBean=(ExcelIpBean) o;
			String orgIp = excelIpBean.getOrgIp();
			String userName = excelIpBean.getUserName();
			String passWord = excelIpBean.getPassWord();
			String desIp = excelIpBean.getDesIp();
			String desPort = excelIpBean.getDesPort();
			Long timeout = excelIpBean.getTimeout();
			String expect1 = excelIpBean.getExpect1();
			String expect2 = excelIpBean.getExpect2();
			String unExpect1 = excelIpBean.getUnExpect1();
			String unExpect2 = excelIpBean.getUnExpect2();
			
			String command="telnet "+desIp+" "+desPort;		
			this.executorService.submit(new Runnable() {
				
				@Override
				public void run() {
					boolean expected = RemoteSSHUtils.isCommandExpected(orgIp, userName, passWord, command, timeout, Arrays.asList(expect1,expect2), Arrays.asList(unExpect1,unExpect2));
					if(!expected) {
						excelIpBean.setResult("不通过");
					}else {
						excelIpBean.setResult("通过");
					}
					taskCount.decrementAndGet();
				}
			});
		}
	}

}
